package com.jingdianjichi.oss.service;

import com.jingdianjichi.oss.adapter.StorageAdapter;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

@Service
public class FileService {

    private final StorageAdapter storageAdapter;

    public FileService(StorageAdapter storageAdapter) {
        this.storageAdapter = storageAdapter;
    }

    /**
     * 列出所有桶
     */
    public List<String> getAllBucket(){
        return storageAdapter.getAllBucket();
    }

    public static void main(String[] args) throws InterruptedException {

            List<Person> people = new ArrayList<>();
            people.add(new Person("Alice", 30));
            people.add(new Person("Bob", 25));
            people.add(new Person("Charlie", 35));
            Collections.sort(people, (o1, o2) -> o1.age - o2.age);

            for (Person person : people) {
                System.out.println(person);
            }

    }


}
class Person {
    private String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }


    public static void main(String[] args) {
        String s="ADOBECODEBANC";
        String t="ABC";
        Map<Character, Integer> tmap = new HashMap<>();
        Map<Character, Integer> smap = new HashMap<>();
            int tleng=t.length();
            int sleng=s.length();

            //保存t串的map集合
            for(int i=0;i<tleng;i++){
                char c=t.charAt(i);
                tmap.put(c,tmap.getOrDefault(c,0)+1);
            }
            //定义快慢指针
            int l=0;
            int r=-1;
            int min=Integer.MAX_VALUE;
            int anl=-1,anr=-1;
            while(r<sleng){
                //快指针移动并判断该字符是否在字串中，在的话放进去
                //不在则不放，但是目前长度加1，
                //然后判断俩map是否相等，相等之后开启二次循环尝试将慢指针向前，判断，当map不等时跳出循环并保存最小字串
                //然后接着外圈
                r++;
                if(r<sleng&&tmap.containsKey(s.charAt(r))){
                    smap.put(s.charAt(r),smap.getOrDefault(s.charAt(r),0)+1);
                }

                while(check(tmap,smap)&&l<=r){
                    if(r-l+1<min){
                        min=l-r+1;
                        anl=l;
                        anr=r+1;
                    }
                    if(tmap.containsKey(s.charAt(l))){
                        smap.put(s.charAt(l),smap.getOrDefault(s.charAt(l),0)-1);
                    }
                    l++;
                }
            }
        }
        public static boolean check(Map<Character,Integer> tmap,Map<Character,Integer> smap){
            Iterator ti=tmap.entrySet().iterator();
            while(ti.hasNext()){
                Map.Entry entry=(Map.Entry) ti.next();
                Character key=(Character) entry.getKey();
                Integer val=(Integer) entry.getValue();
                if(smap.getOrDefault(key,0)< val){
                    return false;
                }
            }
            return true;
        }

    @Override
    public String toString() {
        return name + " (" + age + ")";
    }
}
